package towerDefense.libraries;

import java.awt.Dimension;
import java.io.File;
import java.util.HashMap;
import java.util.ResourceBundle;
import towerDefense.maps.util.LibraryException;
import towerDefense.towers.TowerSprite;
import util.BufferedImageModifier;
import util.libraries.ImageToFileNameLibrary;


public class TowerLibrary extends ImageToFileNameLibrary
{

    private static final String TOWER_PROPERTIES =
        "towerDefense.libraries.towers";
    private static final String DEFAULT_DELIMITER = "\\|";
    HashMap<String, TowerSprite> myTowers;


    public TowerLibrary (File dir)
    {
        super(dir);
    }


    public TowerLibrary (String object, Dimension dim)
    {
        super(object, dim);
    }


    /**
     * returns a clone of the tower that has this string name in the library
     * 
     * @param name
     * @return
     */
    public TowerSprite createTowerByName (String name)
    {
        try
        {
            return (TowerSprite) myTowers.get(name).clone();
        }
        catch (Exception e)
        {
            throw new LibraryException("Not cloneable");
        }
    }


    public TowerSprite checkTowerByName (String name)
    {
        try
        {
            return myTowers.get(name);
        }
        catch (Exception e)
        {
            throw new LibraryException("Not cloneable");
        }
    }


    @Override
    public void buildLibrary (File file)
    {
        super.buildLibrary(file);
        myTowers = new HashMap<String, TowerSprite>();
        String elecheck = "";
        //Allow for each element to have its own class of towers
//        switch(TowerDefense.ELEMENT_TYPE)
//        {
//            case TowerDefense.EARTH_ID:
//                elecheck = "earth";break;
//            case TowerDefense.FIRE_ID:
//                elecheck = "fire";break;
//            case TowerDefense.WATER_ID:
//                elecheck = "water";break;
//            case TowerDefense.WIND_ID:
//                elecheck = "wind";break;
//        }
        ResourceBundle rb = ResourceBundle.getBundle(TOWER_PROPERTIES);
        for (String key : rb.keySet())
            myTowers.put(key, constructTower((String) rb.getObject(key)));
//            if (key.startsWith(elecheck))
//            myTowers.put(key.substring(elecheck.length()), constructTower((String)rb.getObject(key)));
        System.out.println(myTowers.keySet());
    }


    private TowerSprite constructTower (String s)
    {
        String[] info = s.split(DEFAULT_DELIMITER);

        return new TowerSprite(BufferedImageModifier.getScaledInstance(getImageByName(info[0]),
                                                                       (int) myScalar.getWidth(),
                                                                       (int) myScalar.getHeight()),//image
                               Double.parseDouble(info[1]) *
                                       Math.sqrt(Math.pow(myScalar.height, 2) +
                                                 Math.pow(myScalar.width, 2)),//range
                               Integer.parseInt(info[2]),//ShootSpeed
                               TDLibraries.ProjectileLibrary.getProjectileByID(TDLibraries.ProjectileLibrary.getIDByName(info[3])),//projectile
                               Integer.parseInt(info[4])//Cost
        );
    }

}
